﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.ComponentModel;

namespace V5_PhoneAppQianDa {
    public partial class MainPage : PhoneApplicationPage {
        // 构造函数
        public MainPage() {
            InitializeComponent();
            this.BtnStart.Click += new RoutedEventHandler(btnStart_Click);
            this.BtnStop.Click += new RoutedEventHandler(btnStop_Click);
        }

        private BackgroundWorker _bgWork;
        private string _url = string.Empty;
        private bool _isStop = true;
        private string[] _arrName;
        private int _stepTime;
        void btnStart_Click(object sender, RoutedEventArgs e) {
            if (this._isStop) {
                OutResult("正在开启任务!");
                _stepTime = int.Parse("0" + LpStepTime.SelectedItem);
                _url = this.LpUrl.SelectedItem as string;
                if (string.IsNullOrEmpty(_url)) {
                    OutResult("访问地址不能为空!");
                    return;
                }
                string userList = this.TxtUserList.Text;
                if (string.IsNullOrEmpty(userList)) {
                    OutResult("用户列表不能为空!");
                    return;
                }
                _arrName = userList.Split(new string[] { "$$$$" }, StringSplitOptions.RemoveEmptyEntries);
                this._isStop = false;
                _bgWork = new BackgroundWorker();
                _bgWork.DoWork += new DoWorkEventHandler(bgWork_DoWork);
                _bgWork.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWork_RunWorkerCompleted);
                if (!_bgWork.IsBusy) {
                    _bgWork.RunWorkerAsync();
                }
            }
        }

        void bgWork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
            OutResult("线程执行完成!");
        }

        void bgWork_DoWork(object sender, DoWorkEventArgs e) {
            while (true) {
                if (this._isStop) {
                    break;
                }
                if (string.IsNullOrEmpty(_url)) {
                    OutResult("地址为空!请认证填写哦!");
                    break;
                }
                if (_arrName.Length == 0) {
                    break;
                }
                try {
                    //随机取出用户名
                    var rand = new Random();
                    int randNum = rand.Next(0, _arrName.Length - 1);
                    string name = _arrName[randNum];
                    string tempUrl = _url.Replace("${username}", name);
                    //
                    var wc = new WebClient();
                    wc.DownloadStringAsync(new Uri(tempUrl));
                    wc.DownloadStringCompleted += (object sender1, DownloadStringCompletedEventArgs e1) => {
                        var result = name + ":" + DateTime.Now.ToString("ss") + ":" + e1.Result;
                        OutResult(result);
                    };
                }
                catch (Exception ex) {
                    OutResult("有异常请检查哦!" + ex.Message);
                }
                Thread.Sleep(_stepTime);
            }
        }


        void btnStop_Click(object sender, RoutedEventArgs e) {
            if (_bgWork != null && _bgWork.IsBusy) {
                this._isStop = true;
            }
        }

        private void OutResult(string msg) {
            this.Dispatcher.BeginInvoke(new Action(() => {
                this.TbResult.Text = msg;
            }));
        }
    }
}